package br.com.assinchronus.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import br.com.assinchronus.componentes.Casa;

public class RegraUtil {
	
	public static void inserirNoMap(Casa casaKey, Casa casaObrigatoria, Map<Casa, List<Casa>> map) {
		List<Casa> listCasa = null;

		if (map.containsKey(casaKey)) {
			listCasa = map.get(casaKey);
			listCasa.add(casaObrigatoria);
		} else {
			listCasa = new ArrayList<Casa>();
			listCasa.add(casaObrigatoria);
			map.put(casaKey, listCasa);
		}
	}
	
	/**
	 * Clona o Tabuleiro de Casas
	 * 
	 * @param orig
	 *            Tabuleiro original
	 * @return Retorna o Tabuleiro clonado
	 */
	public static Casa[] copy(Object orig) {
		Object obj = null;
		try {
			// Write the object out to a byte array
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(bos);
			out.writeObject(orig);
			out.flush();
			out.close();

			// Make an input stream from the byte array and read
			// a copy of the object back in.
			ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
			obj = in.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
		return (Casa[]) obj;
	}
}
